<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\AdminUser;
use think\Validate;
use app\admin\controller\Main;
class Part extends Main
{
    // 加载 职位列表
    public function partList()
    {
        $name = input('post.name');
        if(empty($name)){
            $data = Db::name('position')->paginate(4);
        }else{
             $data = Db::name('position')->where('position_name','like',"%$name%")->paginate(4);
        }
//        dump($data);die;
        $newarr = array();
        foreach ($data as $v){
            $str = $v['position_cateid'];
            $all = explode(',',$str);
//            dump($all);
            $result = Db::name('category')->field('name')->where('id','in',$all)->select();
//            dump($result);
            $arr = array_column($result,'name');
            $str = implode('/',$arr);
            array_push($newarr,$str);
        }


//die;


        $this->assign('list',$data);
        $this->assign('name',$name);
        $this->assign('str',$newarr);
        return $this->fetch('part/part-list');
    }

    //显示添加
    public function addPart()
    {
        $type = Db::name('category')->where('pid','1')->select();
        $att = Db::name('category')->where('pid','2')->select();
        $region = Db::name('category')->where('pid','3')->select();

        $this->assign('type',$type);
        $this->assign('att',$att);
        $this->assign('region',$region);
    	return $this->fetch('part/part-add');
    }

    // 执行 添加 职位
    public function add()
    {
        $data = input('post.');
        $res['position_name'] = $data['name'];
        $res['position_start'] = strtotime($data['start']);
        $res['position_end'] = strtotime($data['end']);
        $res['position_num'] = $data['num'];
        $res['position_require'] = $data['require'];
        $res['position_tel'] = $data['phone'];
        $res['position_place'] = $data['place'];
        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
        $res['position_publish'] = time();
        $res['position_companyid'] = $data['companyid'];
        $res['position_payway'] = $data['payway'];

        $validate = new Validate([
            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
        ]);
        $phone = [
            'phone' => $data['phone']
        ];
        if (!$validate->check($phone)) {
            dump($validate->getError());
        }

        $result = Db::name('position')->insert($res);

        if ($result == 0) {
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
            return $this->error($result);
        } else {
            return $this->success($result);
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
        }

    }


    //删除职位
    public function delPart($id)
    {
        $result = Db::name('position')->delete($id);
         if ($result > 0) {
            $res = Db::name('position')->where('aid',$id)->delete($id);
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的职位删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的职位删除失败!';
        }

        return json($info);
    }


    //显示兼职详情
    public function roleMessage($id)
    {

        $data = Db::name('position')->where('position_id',$id)->find();
//        dump($data); die;
        $data['position_start'] = date('Y-m-d',$data['position_start']);
        $data['position_end'] = date('Y-m-d',$data['position_end']);
        $data['position_publish'] = date('Y-m-d H:i:s',$data['position_publish']);

        $this->assign('list',$data);
        return $this->fetch('partjob/part-message');
    }

    //修改 通过 和 下线

    public function partstatus($id,$status)
    {
        if ($status == 1){
            $result = Db::name('position')->where('position_id',$id)->update(['position_status'=>'2']);
            if($result){
                $this->success('已成功上线！');
            } else {
                $this->error('修改失败');
            }
        }

        if ($status == 2){
            $result = Db::name('position')->where('position_id',$id)->update(['position_status'=>'3']);
            if($result){
                $this->success('该职位已下线！');
            } else {
                $this->error('修改失败');
            }
        }

        if ($status == 3){
            $this->error('该职位已下线');
        }

        return $id;
    }


        // 显示编辑页面
    public function editList($id)
    {
        $data = Db::name('position')->find($id);

        $type = Db::name('category')->where('pid','1')->select();
        $att = Db::name('category')->where('pid','2')->select();
        $region = Db::name('category')->where('pid','3')->select();
//        dump($type); die;
        $this->assign('data',$data);
        $this->assign('type',$type);
        $this->assign('att',$att);
        $this->assign('region',$region);
        return $this->fetch('part/part-edit');
    }

        // 执行编辑
    public function partEdit($id)
    {

        $data = input('post.');
//        dump($data);
        $res['position_name'] = $data['name'];
        $res['position_start'] = strtotime($data['start']);
        $res['position_end'] = strtotime($data['end']);
        $res['position_num'] = $data['num'];
        $res['position_require'] = $data['require'];
        $res['position_tel'] = $data['phone'];
        $res['position_place'] = $data['place'];
        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
//        dump($res); die;


        $validate = new Validate([
            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
        ]);
        $phone = [
            'phone' => $data['phone']
        ];
        if (!$validate->check($phone)) {
            dump($validate->getError());
        }

        $result = Db::name('position')->where('position_id',$id)->update($res);

        if ($result == 0) {
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
            return $this->error($result);
        } else {
            return $this->success($result);
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
        }



    }
}
